﻿Public Class FrmSaatToplam

    Sub New()
        InitializeComponent()
        ToolBar.Renderer = New FirefoxRenderer
        BtnHelp.DropDown = HelpMenu
        List.AddColumn("personel", "Personel", "60%", "l")
        List.AddColumn("bakiye", "Bakiye", "20%", "c")
        List.AddColumn("kullanilan", "Kullanılan", "20%", "c")
        List.AddColumn("toplam", "Toplam", "20%", "c")
        List.AddColumn("gun", "Gün", "20%", "c")
        List.AddColumn("kalan", "Kalan", "20%", "c")
        List.CellBorderStyle = DataGridViewCellBorderStyle.Single
        BtnProcess.Enabled = Kullanici.[Operator]
        Icon = Icon.FromHandle(My.Resources.saat.GetHicon)
        Reload()
    End Sub

    Sub Reload()
        List.Rows.Clear()
        Dim c As Integer = 0
        For Each P In Kullanici.Liste(Kullanici.OprBirim)
            List.Rows.Add()
            Dim Row As DataGridViewRow = List.Rows(List.Rows.Count - 1)
            Row.Tag = P.Kod
            Row.Cells("personel").Value = P.Ad
            Hesapla(P.Kod, Row)
            If Trim(Row.Cells("gun").Value) <> "-" Then c += 1
        Next
        If c > 0 Then InfoBox.ShowMessage("i", c & " adet personelin sekiz saati aşkın birikmiş saatlik izinleri vardır.")
    End Sub

    Sub Hesapla(ByVal Personel As Integer, ByVal Row As DataGridViewRow)
        Dim Kullanilan As Decimal = 0
        Dim Liste = Service.Izin.SaatlikListe(Personel)
        Dim Bakiye As Decimal = 0
        For Each Item In Liste
            If Item.Saat1 = "" Then
                Bakiye = Item.SaatBakiyesi
            Else
                Kullanilan += SaatHesapla(Item.Saat1, Item.Saat2)
            End If
        Next
        Dim Toplam = Kullanilan + Bakiye
        Dim Gun = Int(Toplam / 8)
        Dim Kalan = Toplam - 8 * Gun
        If Toplam < 8 Then
            With Row.DefaultCellStyle
                .ForeColor = Color.Gray
                .Font = New Font(List.Font, FontStyle.Italic)
                .SelectionForeColor = Color.Gray
            End With
        End If
        Row.Cells("bakiye").Value = If(Bakiye = 0, "-", FormatSaat(Bakiye))
        Row.Cells("kullanilan").Value = If(Kullanilan = 0, "-", FormatSaat(Kullanilan))
        Row.Cells("toplam").Value = If(Toplam = 0, "-", FormatSaat(Toplam))
        Row.Cells("gun").Value = If(Gun = 0, "-", Gun)
        Row.Cells("kalan").Value = If(Kalan = 0, "-", FormatSaat(Kalan))
        Row.Cells("kalan").Tag = Kalan
    End Sub

    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Close()
    End Sub

    Private Sub BtnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcess.Click
        Dim Liste As New List(Of DataGridViewRow)
        For Each Row As DataGridViewRow In List.Rows
            If Trim(Row.Cells("gun").Value) <> "-" Then Liste.Add(Row)
        Next
        If Liste.Count = 0 Then
            Warning("Sekiz saat veya daha üstü saatlik izni birikmiş personel yok.")
            Exit Sub
        End If
        If Not Confirm(Liste.Count & " adet personelin saatlik izinleri yıllık izinlerinden düşülecek. İşleme devam etmek istiyor musunuz?") Then Exit Sub
        For Each Row In Liste
            Service.Izin.SaatlikTopluKaydet(0, Row.Tag, Today.Date, Row.Cells("gun").Value, Row.Cells("kalan").Tag, "", Kullanici.Kod)
        Next
        Info("Personelin saatlik izinleri yıllık izinlerinden düşülmüştür.")
        Close()
    End Sub

    Private Sub ToolBar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolBar.Paint
        FirefoxRenderer.PaintBackground(e.Graphics, sender.ClientRectangle)
    End Sub

End Class